import http from '../../utils/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:0,
    data:[],
    select:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getnum()
    let islogin=wx.getStorageSync('islogin')
    this.setData({
      islogin
    })
    wx.request({
      url: 'https://www.shumutou.com/api/index/recharge',
      success:(res)=>{
        console.log(res);
        this.setData({
          data:res.data.data
        })
        
      }
    })
  },
   getnum(){
    var uid = wx.getStorageSync('uid');
    console.log(uid);
    http('/index/index','post',{uid}).then(res=>{
      console.log(res);
      this.setData({
        number:res.data.data.number
      })
    }).catch(res=>{
      console.log(res);
    })
   },
  pay:function(){
    var _this = this;
    var type = this.data.type;
    if(!type){
      wx.showToast({  
        title: '请选择购买',  
        icon: 'error',  
        duration: 2000  
      }) 
      return;
    }
  
    let uid = wx.getStorageSync("uid");
    wx.showLoading({
        title: '加载中',
    })
    wx.request({
      url: 'https://www.shumutou.com/api/index/pay',
      method:'post',
      data: {
        uid: uid,
        lid:type,
      },
      success:(res)=>{
        if(res.data.success == 1){
          wx.request({
            url: 'https://www.shumutou.com/api/pay/index',
            method:'post',
            data: {
              code: res.data.data,
            },
            success:(res2)=>{
              console.log(res2)
              if(res2.data.success == 1){
                wx.requestPayment({
                  timeStamp: res2.data.data.timestamp,
                  nonceStr: res2.data.data.nonceStr,
                  package: res2.data.data.package,
                  signType: 'MD5',
                  paySign: res2.data.data.paySign,
                  success (res) {
                    wx.hideLoading()
                    wx.showToast({  
                      title: '支付成功',  
                      icon: 'success',  
                      duration: 2000  
                    }) 
                  _this.getnum()
                  },
                  fail (res) {
                    console.log(res)
                    wx.showToast({  
                      title: '支付失败',  
                      icon: 'error',  
                      duration: 2000  
                    }) 
                  }
                })
              }else{
                wx.showToast({  
                  title: res.tips,  
                  icon: 'error',  
                  duration: 2000  
                }) 
              }
              console.log(res);
              // this.setData({
              // })
            }
          })
        }else{
          wx.showToast({  
            title: res.tips,  
            icon: 'error',  
            duration: 2000  
          }) 
        }
      }
    })
  },
  money:function(event){
    let index=event.currentTarget.dataset.index;
    let  select=event.currentTarget.dataset.type
    if(this.data.type != index){
      this.setData({
        type: index,
        select
      })
    }
  }
})